package com.noah.project.controller

import com.noah.project.global.Const
import com.noah.project.global.JsonManager
import com.noah.project.dao.MarkDao
import com.noah.project.global.TokenManager
import com.noah.project.model.Mark
import com.noah.project.model.Reply
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
class MarkApiController {
    @Autowired
    lateinit var markDao: MarkDao
    val log = LoggerFactory.getLogger(MarkApiController::class.simpleName)

    /*****************关注******************/
    @RequestMapping(Const.API_GET_MARK_NOTE_LIST)
    fun getMarkNoteList(mid: Long, time: Long, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            val markNotes = markDao.findMarkNotes(mid, time)
            if (markNotes.isNotEmpty()) {
                return Reply(code = Const.SUCCESS, msg = "获取关注列表成功", data = JsonManager.toJson(markNotes))
            }
            return Reply(code = Const.FAIL, msg = "没有更多关注")
        } catch (e: Exception) {
            log.error("关注列表", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "关注异常")
        }
    }

    @RequestMapping(Const.API_DELETE_MARK)
    fun deleteMark(mark: Mark, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            if (markDao.findMark(mark.mid, mark.nid) == null) {
                return Reply(code = Const.ERROR_ALREADY, msg = "已经取消关注")
            }
            if (markDao.delete(mark) > 0) {
                return Reply(code = Const.SUCCESS, msg = "取消关注")
            }
            return Reply(code = Const.FAIL, msg = "取消关注失败")
        } catch (e: Exception) {
            log.error("取消关注", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "关注异常")
        }
    }

    @RequestMapping(Const.API_ADD_MARK)
    fun addMark(mark: Mark, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            if (markDao.findMark(mark.mid, mark.nid) != null) {
                return Reply(code = Const.ERROR_ALREADY, msg = "已经关注")
            }
            mark.mTime = System.currentTimeMillis()
            if (markDao.insert(mark) > 0) {
                return Reply(code = Const.SUCCESS, msg = "关注成功")
            }
            return Reply(code = Const.FAIL, msg = "关注失败")
        } catch (e: Exception) {
            log.error("关注日记", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "关注异常")
        }
    }

    @RequestMapping(Const.API_GET_MARK)
    fun getMark(mark: Mark, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            if (markDao.findMark(mark.mid, mark.nid) != null) {
                return Reply(code = Const.SUCCESS, msg = "已经关注")
            } else {
                return Reply(code = Const.FAIL, msg = "还没关注")
            }
        } catch (e: Exception) {
            log.error("关注日记", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "关注异常")
        }
    }
}